import java.io.*;

import java.net.*;

import java.security.MessageDigest;


public class Client3 {

    public static void main(String args[]) throws Exception {



        MyBC turner = new MyBC();



        Skey_DES skey_des = new Skey_DES();

        skey_des.key_DES();

        Skey_kb skey_kb = new Skey_kb();

        skey_kb.key();

        /*产生密钥*/



        SEnc sEnc = new SEnc();



        try {

            Socket socket = new Socket("127.0.0.1", 4700);

//向本机的4700端口发出客户请求


            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

//由系统标准输入设备构造BufferedReader对象


            PrintWriter os = new PrintWriter(socket.getOutputStream());

//由Socket对象得到输出流，并构造PrintWriter对象


            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));

//由Socket对象得到输入流，并构造相应的BufferedReader对象




            String readline;



            readline =sin.readLine();

            //从系统标准输入读入一字符串
            String x = readline;



            // 将客户端明文的Hash值传送给服务器


            MessageDigest m2 = MessageDigest.getInstance("MD5");

            m2.update(x.getBytes());

            byte a[] = m2.digest();

            String result = "";

            for (int i = 0; i < a.length; i++) {

                result += Integer.toHexString((0x000000ff & a[i]) | 0xffffff00).substring(6);

            }

            System.out.println("明文MD5值为:" + result);

            os.println(result);//通过网络将明文的Hash函数值传送到服务器




            // String str = is.readLine();// 从网络输入流读取结果


            System.out.println("从服务器接收到的结果为：" + result); // 输出服务器返回的结果




            while (!readline.equals("bye")) {

//若从标准输入读入的字符串为 "bye"则停止循环




                readline = SEnc.Enc(turner.turn(readline));



                os.println(readline);

                /*把中缀表达式调用MyBC.java的功能转化为后缀表达式，把后缀表达式用3DES加密后通过网络发送给服务器*/



                //将从系统标准输入读入的字符串输出到Server


                os.flush();



                //刷新输出流，使Server马上收到该字符串


                System.out.println("Client:" + readline);



                //在系统标准输出上打印读入的字符串


                System.out.println("Server:" + is.readLine());



                //从Server读入一字符串，并打印到标准输出上


                readline = sin.readLine(); //从系统标准输入读入一字符串




            } //继续循环


            os.close(); //关闭Socket输出流


            is.close(); //关闭Socket输入流


            socket.close(); //关闭Socket


        } catch (Exception e) {

            System.out.println("Error" + e); //出错，则打印出错信息


        }

    }

}